package com.libui.thirdpartsdk.systemsdk

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import com.libui.BaseApplication
import com.libui.R
import com.libui.thirdpartsdk.SDKInterface
import com.libui.thirdpartsdk.share.ShareCompletion
import com.libui.thirdpartsdk.share.ShareInterface
import com.libui.thirdpartsdk.share.models.ShareObjectType
import com.libui.thirdpartsdk.share.models.ShareUrlObject
import com.libui.toast.Toast

class CopyShare : ShareInterface {
    companion object {
        val builder = object : ShareInterface.Builder {
            override val type: String = "copy"

            override val title: String = "复制链接"

            override val icon: String = "copy.png"

            override val selectedIcon: String? = null

            override fun build(sdk: SDKInterface): ShareInterface {
                return CopyShare()
            }
        }
    }

    override val shareObjectTypes: List<ShareObjectType> = listOf(ShareObjectType.URL)

    override val type: String
        get() = builder.type

    override val title: String
        get() = builder.title

    override val icon: String?
        get() = builder.icon

    override val selectedIcon: String?
        get() = builder.selectedIcon

    override var isSelected: Boolean = false

    override fun shareUrl(shareObject: ShareUrlObject, completion: ShareCompletion?) {
        BaseApplication.instance?.currentNoPresentActivity?.let { context ->
            val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            cm.setPrimaryClip(ClipData.newPlainText(null, shareObject.url))

            Toast.ToastBuilder(context.resources.getString(R.string.copy_success))
                .setInteractiveHide(true).setDuration(800).build(context).show()

            completion?.let { it(true) }
        } ?: run {
            completion?.let { it(false) }
        }
    }
}